﻿// =================================================================== 
// 项目说明
//====================================================================
// Wup。@Copy Right 2014
// 文件： type.cs
// 项目名称：买车网
// 创建时间：2014/7/20
// 负责人：Wup
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;


namespace Weixin.DAL.ChinaTax
{
    /// <summary>
    /// 数据层实例化接口类 dbo.type.
    /// </summary>
    public partial class typeDataAccessLayer 
    {
		#region 构造函数


		/// <summary>
		/// 
		/// </summary>
		public typeDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_typeModel">type实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(typeEntity _typeModel)
		{
			string sqlStr="insert into type([typename],[typeid],[content]) values(@typename,@typeid,@content) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@typename",SqlDbType.VarChar),
			new SqlParameter("@typeid",SqlDbType.Int),
			new SqlParameter("@content",SqlDbType.VarChar)
			};			
			_param[0].Value=_typeModel.typename;
			_param[1].Value=_typeModel.typeid;
			_param[2].Value=_typeModel.content;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_typeModel">type实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(SqlTransaction sp,typeEntity _typeModel)
		{
			string sqlStr="insert into type([typename],[typeid],[content]) values(@typename,@typeid,@content) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@typename",SqlDbType.VarChar),
			new SqlParameter("@typeid",SqlDbType.Int),
			new SqlParameter("@content",SqlDbType.VarChar)
			};			
			_param[0].Value=_typeModel.typename;
			_param[1].Value=_typeModel.typeid;
			_param[2].Value=_typeModel.content;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(sp,CommandType.Text,sqlStr,_param));
			return res;
		}


		/// <summary>
		/// 向数据表type更新一条记录。
		/// </summary>
		/// <param name="_typeModel">_typeModel</param>
		/// <returns>影响的行数</returns>
		public int Update(typeEntity _typeModel)
		{
            string sqlStr="update type set [typename]=@typename,[typeid]=@typeid,[content]=@content where id=@id";
			SqlParameter[] _param={				
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@typename",SqlDbType.VarChar),
				new SqlParameter("@typeid",SqlDbType.Int),
				new SqlParameter("@content",SqlDbType.VarChar)
				};				
				_param[0].Value=_typeModel.id;
				_param[1].Value=_typeModel.typename;
				_param[2].Value=_typeModel.typeid;
				_param[3].Value=_typeModel.content;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表type更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_typeModel">_typeModel</param>
		/// <returns>影响的行数</returns>
		public int Update(SqlTransaction sp,typeEntity _typeModel)
		{
            string sqlStr="update type set [typename]=@typename,[typeid]=@typeid,[content]=@content where id=@id";
			SqlParameter[] _param={				
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@typename",SqlDbType.VarChar),
				new SqlParameter("@typeid",SqlDbType.Int),
				new SqlParameter("@content",SqlDbType.VarChar)
				};				
				_param[0].Value=_typeModel.id;
				_param[1].Value=_typeModel.typename;
				_param[2].Value=_typeModel.typeid;
				_param[3].Value=_typeModel.content;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}		
		
		
		/// <summary>
		/// 删除数据表type中的一条记录
		/// </summary>
	    /// <param name="id">id</param>
		/// <returns>影响的行数</returns>
		public int Delete(int id)
		{
			string sqlStr="delete from type where [id]=@id";
			SqlParameter[] _param={			
			new SqlParameter("@id",SqlDbType.Int),
			
			};			
			_param[0].Value=id;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表type中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="id">id</param>
		/// <returns>影响的行数</returns>
		public int Delete(SqlTransaction sp,int id)
		{
			string sqlStr="delete from type where [id]=@id";
			SqlParameter[] _param={			
			new SqlParameter("@id",SqlDbType.Int),
			
			};			
			_param[0].Value=id;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  type 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>type 数据实体</returns>
		public typeEntity Populate_typeEntity_FromDr(DataRow row)
		{
			typeEntity Obj = new typeEntity();
			if(row!=null)
			{
				Obj.id = (( row["id"])==DBNull.Value)?0:Convert.ToInt32( row["id"]);
				Obj.typename =  row["typename"].ToString();
				Obj.typeid = (( row["typeid"])==DBNull.Value)?0:Convert.ToInt32( row["typeid"]);
				Obj.content =  row["content"].ToString();
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  type 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>type 数据实体</returns>
		public typeEntity Populate_typeEntity_FromDr(IDataReader dr)
		{
			typeEntity Obj = new typeEntity();
			
				Obj.id = (( dr["id"])==DBNull.Value)?0:Convert.ToInt32( dr["id"]);
				Obj.typename =  dr["typename"].ToString();
				Obj.typeid = (( dr["typeid"])==DBNull.Value)?0:Convert.ToInt32( dr["typeid"]);
				Obj.content =  dr["content"].ToString();
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个type对象
		/// </summary>
		/// <param name="id">id</param>
		/// <returns>type对象</returns>
		public typeEntity Get_typeEntity (int id)
		{
			typeEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@id",SqlDbType.Int)
			};
			_param[0].Value=id;
			string sqlStr="select * from type with(nolock) where id=@id";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_typeEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表type所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< typeEntity> Get_typeAll()
		{
			IList< typeEntity> Obj=new List< typeEntity>();
			string sqlStr="select * from type";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_typeEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>是/否</returns>
		public bool IsExisttype(int id)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@id",SqlDbType.Int)
                                  };
            _param[0].Value=id;
            string sqlStr="select Count(*) from type where id=@id";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
